package 
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import fl.containers.ScrollPane;
	
	public class ExamFrame extends MovieClip
	{
		private var load:Loader = new Loader();
		private var nowExam:DisplayObject;
		private var TIME:int = 1200;
		private var t:Timer;
		
		private var select:int = 0;
		
		private var scroeArr:Array;
		
		private var isOver:Boolean = false;
		
		private var htmlData:Object;
		
		//private var urls:Array;
		
		private var scrollP:ScrollPane;
		private var scrollt:ScrollPane;
		
		private var ti:Array;
		
		private var tiTxt:TextField;
		
		/**
		 * 是否为真实时间
		 */
		private var isTrue:Boolean = false;
		
		public function ExamFrame()
		{
			
			scrollP = this.scroll;
			scrollt = this.scrolltxt;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			load.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoads);
			load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			load.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			TiaoGuo.addEventListener(MouseEvent.CLICK, onTiaoGuo);
			TiJiao.addEventListener(MouseEvent.CLICK, onTiJiao);
			JiaoJuan.addEventListener(MouseEvent.CLICK, onJiaoJuan);
			notice.visible = false;
			htmlData = loaderInfo.parameters;
			
			if(htmlData["TestTime"])
			{
				TIME = int(htmlData["TestTime"]) * 60;
				isTrue = true;
			}
			
			if(htmlData["Content"])
			{
				ti = (htmlData["Content"] as String).split("_");
			}
			initUrl();
			scroeArr = new Array();
			tiTxt = new TextField();
			tiTxt.selectable = false;
			tiTxt.wordWrap = true;
			tiTxt.multiline = true;
			tiTxt.autoSize = TextFieldAutoSize.LEFT;
			tiTxt.width = scrollt.width - 20;
			tiTxt.text = "11111111111111111111111111111";
			t = new Timer(1000);
			t.addEventListener(TimerEvent.TIMER, onTime);
			t.start();
			showNotice(loaderInfo.url);
			if(isTrue)//
			{
				initXML();
				//showNotice("html参数 time:" + TIME);
			}
			else
			{
				showNotice("没接到html参数!!!!!");
			}
		}
		/**
		 * 类型数组
		 */
		private var urlTypeArr:Array = new Array();
		private function initUrl():void
		{
			urlTypeArr.push(loaderInfo.parameters["A"]);//
			urlTypeArr.push(loaderInfo.parameters["B"]);//5)//
			urlTypeArr.push(loaderInfo.parameters["C"]);
			urlTypeArr.push(loaderInfo.parameters["D"]);
			urlTypeArr.push(loaderInfo.parameters["E"]);
			urlTypeArr.push(loaderInfo.parameters["F"]);
			urlTypeArr.push(loaderInfo.parameters["G"]);
			urlTypeArr.push(loaderInfo.parameters["H"]);
		}
		private var examUrlArr:Array = new Array();
		private function radomUrl():void
		{
			//showNotice("load xml ove2r" + urlTypeArr.length);
			for(var i:int = 0;i<urlTypeArr.length;i++)
			{
				if(urlTypeArr[i] == null)
					continue;
				
				for(var j:int = 0;j<urlTypeArr[i];j++)
				{
					var arr:Array = qData[i] as Array;
					if(arr.length <= 0) continue;
					var radom:int = Math.floor(Math.random() * arr.length);
					var url:String = arr[radom];
					trace(radom + "   " + arr.length + "url1--------"+url);
					arr.splice(radom,1);
					examUrlArr.push(url);
					trace("url2--------"+url);
					//ExternalInterface.call("sendScroe", "url" + (examUrlArr.length - 1) + "=" + url);
				}
			}
			
			for(var i:int = 1;i<= examUrlArr.length;i++)
			{
				var anniu:Anniu = new Anniu();
				anniu.name = "btn_" + i;
				trace(123123123);
				anniu.setLabel(i.toString());
				trace(123123123);
				anniu.setIndex(i);
				trace(123123123);
				anniu.setState(1);
				trace("Label"+i);
				anniu.x = 112 + (i - 1) * (anniu.width + 5);
				anniu.y = 95;
				anniu.addEventListener(MouseEvent.CLICK, onSelect);
				addChild(anniu);
			}
			changeExam(1);
		}
		
		private function onJiaoJuan(e:MouseEvent):void
		{
			var scroe:int = 0;
			var scroeStr:String = "";
			
			for(var i:int = 1;i<= 10;i++)
			{
				if(scroeArr[i])
				{
					scroe += scroeArr[i];
					scroeStr += i + "、" +scroeArr[i] + "  ";
				}
			}
			//showNotice("本次答题得分为:" + scroeStr);
			isOver = true;
			this.mouseChildren = false;
			this.mouseEnabled = false;
//			t.stop();
			
			if(ExternalInterface.available)
			{
				ExternalInterface.call("handUpPoint", "ExScore="+scroe+"&userId="+htmlData["userId"]+"&spid="+htmlData["spid"]+"&spegid="+htmlData["spegid"]);//htmlData["userId"] + "_" +  htmlData["spegid"] + "_" +  htmlData["spid"] + "_" +  scroe);
			}
			else
			{
				showNotice("ExternalInterface 不可用");
			}
		}
		
		private function onTiJiao(e:MouseEvent):void
		{
			var obj:Object = nowExam["getScroe"]();
			scroeArr[obj["id"]] = obj["scroe"];
			var anniu:Anniu = this.getChildByName("btn_" + select) as Anniu;
			anniu.setState(3);
			(nowExam as MovieClip).mouseChildren = false;
		}
		
		private function onTiaoGuo(e:MouseEvent):void
		{
			changeExam(select + 1);
		}
		
		private function onError(e:IOErrorEvent):void
		{
			showNotice("加载" +select+ "出错!" + (e.currentTarget as LoaderInfo).url);
		}
		
		private function onSelect(e:MouseEvent):void
		{
			var s:MovieClip = e.currentTarget as MovieClip;
			if(s.currentFrame == 3)
			{
				showNotice("此题以提交!");
				return;
			}
			else
			{
				changeExam(s.name.split("_")[1]);
			}
		}
		
		private function changeExam(s:int):void
		{
			if(s == select)
				return;
			if(s > examUrlArr.length)
			{
				s = 1;
			}
			var anniu:Anniu = this.getChildByName("btn_" + select) as Anniu;
			if(anniu != null)
			{
				if(anniu.currentFrame == 2)
				{
					anniu.setState(1);
				}
			}
			select = s;
			anniu = this.getChildByName("btn_" + select) as Anniu;
			anniu.setState(2);
			loadExam(select - 1);
//			loadExam(urls[select - 1]);
		}
		
		private function onTime(e:TimerEvent):void
		{
			var tH:int = Math.floor(TIME / 3600);
			var tM:int = Math.floor((TIME - tH * 3600) / 60);
			var ts:int = TIME - tH * 3600 - tM * 60;
			this.ShiJian.text = "0" + tH + ":" + (tM.toString().length <= 1 ? ("0" + tM) : tM) + ":" + (ts.toString().length <= 1 ? ("0" + ts) : ts);
			TIME--;
			if(TIME <= 0)
			{
				trace("时间到!!!");
			}
		}
		
		private function onLoads(e:Event):void
		{
			trace(11111111111111);
			nowExam = (e.currentTarget as LoaderInfo).content;
			nowExam.addEventListener(ProEvent.SEND_SCORE, onGetScore);
			nowExam.addEventListener(ProEvent.SEND_NOTICE, onShowNotice);
			scrollP.source = nowExam;
//			scrollP.addChild(nowExam);
			scrollP.update();
			//addChildAt(nowExam, 0);
//			nowExam.x = 86;
//			nowExam.y = 80;
			if(ti && ti[select - 1])
			{
				tiTxt.text = ti[select - 1];
				scrollt.source = tiTxt;
				scrollt.update();
			}
			else
			{
				tiTxt.text = "1111111231241421111" + select;
				scrollt.source = tiTxt;
				scrollt.update();
				trace(tiTxt.text);
			}
		}
		
		public function loadExam(url:int):void
		{
			if(nowExam && nowExam.parent)
			{
				nowExam.parent.removeChild(nowExam);
			}
//			if(load.contentLoaderInfo.bytesLoaded > 0)
//			{
//				load.unload();
//			}
//			load.load(new URLRequest(urls[url-1] + ".swf"), new LoaderContext(false,ApplicationDomain.currentDomain));
			trace(url + "load   " + examUrlArr[url]);
			load.load(new URLRequest(examUrlArr[url]), new LoaderContext(false,ApplicationDomain.currentDomain));
//			load.load(new URLRequest(urls[url-1] + ".swf"), new LoaderContext(false,ApplicationDomain.currentDomain));
		}
		
		private function onProgress(e:ProgressEvent):void
		{
			trace(e.bytesLoaded + "/" + e.bytesTotal);
		}
		
		private function onGetScore(e:ProEvent):void
		{
			trace(e.data["id"]);
			trace(e.data["score"]);
			scroeArr[e.data["id"]] = e.data["score"];
		}
		
		private function onShowNotice(e:ProEvent):void
		{
			trace("get 1");
			showNotice(e.data["str"]);
		}
		
		private function onNoticeClose(e:MouseEvent):void
		{
			notice.visible = false;
			notice.Close.removeEventListener(MouseEvent.CLICK, onNoticeClose);
			notice.OK.removeEventListener(MouseEvent.CLICK, onNoticeClose);
		}
		
		private function showNotice(str:String):void
		{
			trace(123123123,"--");
			notice.cont.text = str;
			notice.visible = true;
			notice.Close.addEventListener(MouseEvent.CLICK, onNoticeClose);
			notice.OK.addEventListener(MouseEvent.CLICK, onNoticeClose);
		}
		
		private var loadtxt:URLLoader;
		private function initXML():void
		{
			loadtxt = new URLLoader();
			loadtxt.addEventListener(Event.COMPLETE, onCom);
			loadtxt.load(new URLRequest("source/kind.xml"));//"http://192.168.1.102:8080/Learing/kind.xml"));
		}
		private var qData:Dictionary = new Dictionary();
		private function onCom(e:Event):void
		{
			var uloader:URLLoader = e.currentTarget as URLLoader;
			var xml:XML = XML(uloader.data);
			//trace(xml.kind);
			//showNotice("load xml over");
			//var 
			var i:int = 0;
			for each(var s:Object in xml.kind)
			{
				for each(var str:String in xml.kind[i]["question"])
				{
					trace(i + "  "+str);
					if(qData[i] == null)
					{
						qData[i] = new Array();
					}
					(qData[i] as Array).push(str);
				}
				i++;
			}
			
			
			//showNotice("load xml ove2r");
			radomUrl();
		}
	}
}